package rdbms;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import DAO.DatabasePool;
import DAO.ENSequenceDAO;


import model.DIDate;
import model.ENSequence;

public class RDBMSDIDateDAO {

	private String idConnection;
	private ENSequenceDAO sequence;
	private final String name = "DATE";

	private static String INSERT = "insert into DI_DATE (TIME_ID, DAY, MONTH, YEAR ) values (?, ?, ?, ?)";

	private static String DELETE = "delete from DI_DATE where TIME_ID = ? ";

	private static String DELETE_ALL = "delete from DI_DATE";

	private static String SELECT_BY_ID = "select DAY, MONTH, YEAR "
			+ "from DI_DATE where TIME_ID = ? ";

	private static String SELECT_ALL = "select * from DI_DATE";

	private static String UPDATE = "update DI_DATE set DAY = ?, MONTH = ?, YEAR = ? where TIME_ID = ?";

	public RDBMSDIDateDAO(final String idConnection, final ENSequenceDAO sequence) throws SQLException {
		this.idConnection = idConnection;
		this.sequence = sequence;
	}
	
	public boolean insert(DIDate diDate) throws Exception {
		boolean success = false;
		Connection conn = null;
		PreparedStatement pst = null;
		try {
			conn = DatabasePool.getConnection(idConnection);
			diDate.setTimeId(sequence.nextval(ENSequence.DATE_ID));
			pst = conn.prepareStatement(INSERT);
			pst.setInt(1, diDate.getTimeId());
			pst.setInt(2, diDate.getDay());
			pst.setInt(3, diDate.getMonth());
			pst.setInt(4, diDate.getYear());
			pst.execute();
			success = true;
		} catch (Exception e) {
			throw new Exception("Exception while inserting "+name, e);
		} finally {
			if (success) {
				conn.commit();
				System.out.println(name+" Insert - Commit");
			} else {
				conn.rollback();
				System.out.println(name+" Insert - Rollback");
			}
			if (pst != null)
				pst.close();
		}
		return success;
	}

	public boolean delete(int id) throws Exception {
		boolean success = false;
		Connection conn = null;
		PreparedStatement pst = null;
		try {
			conn = DatabasePool.getConnection(idConnection);
			pst = conn.prepareStatement(DELETE);
			pst.setInt(1, id);
			pst.execute();
			success = true;
		} catch (Exception e) {
			throw new Exception("Exception while deleting "+name, e);
		} finally {
			if (success) {
				conn.commit();
				System.out.println(name+" - Delete - Commit");
			} else {
				conn.rollback();
				System.out.println(name+" - Delete - Rollback");
			}
			if (pst != null)
				pst.close();
		}
		return success;
	}

	public boolean deleteAll() throws Exception {
		boolean success = false;
		Connection conn = null;
		PreparedStatement pst = null;
		try {
			conn = DatabasePool.getConnection(idConnection);
			pst = conn.prepareStatement(DELETE_ALL);
			pst.execute();
			success = true;
		} catch (Exception e) {
			throw new Exception("Exception while deleting(all) "+name, e);
		} finally {
			if (success) {
				conn.commit();
				System.out.println(name+" - Delete(all) - Commit");
			} else {
				conn.rollback();
				System.out.println(name+" - Delete(all) - Rollback");
			}
			if (pst != null)
				pst.close();
		}
		return success;
	}

	public DIDate find(int id) throws Exception {
		Connection conn = null;
		PreparedStatement pst = null;
		ResultSet rss = null;
		DIDate diDate = null;
		try {
			conn = DatabasePool.getConnection(idConnection);
			pst = conn.prepareStatement(SELECT_BY_ID);
			pst.setInt(1, id);
			rss = pst.executeQuery();
			if (rss.next()) {
				diDate = new DIDate(id, rss.getInt(1), rss.getInt(2), rss.getInt(3));
			}
			return diDate;
		} catch (Exception e) {
			throw new Exception("Exception while retrieving "+name, e);
		} finally {
			if (rss != null)
				rss.close();
			if (pst != null)
				pst.close();
		}
	}

	public boolean update(DIDate diDate) throws Exception {
		boolean success = false;
		Connection conn = null;
		PreparedStatement pst = null;
		try {
			conn = DatabasePool.getConnection(idConnection);
			pst = conn.prepareStatement(UPDATE);
			pst.setInt(1, diDate.getDay());
			pst.setInt(2, diDate.getMonth());
			pst.setInt(3, diDate.getYear());
			pst.setInt(4, diDate.getTimeId());
			System.out.println(pst);
			pst.execute();
			success = true;
		} catch (Exception e) {
			throw new Exception("Exception while updating "+name, e);
		} finally {
			if (success) {
				conn.commit();
				System.out.println(name+" - Update - Commit");
			} else {
				conn.rollback();
				System.out.println(name+" - Update - Rollback");
			}
			if (pst != null)
				pst.close();
		}
		return success;
	}
	
	
	public List<DIDate> list(int noi) throws Exception {
		List<DIDate> list = null;
		Connection conn = null;
		PreparedStatement pst = null;
		ResultSet rss = null;
		int ctr = 0;
		try {
			conn = DatabasePool.getConnection(idConnection);
			list = new ArrayList<DIDate>();
			pst = conn.prepareStatement(SELECT_ALL);
			rss = pst.executeQuery();
			while (rss.next()) {
				if (++ctr > noi)
					break;
				DIDate diDate = new DIDate(rss.getInt(1), rss.getInt(2), rss.getInt(3),rss.getInt(4));
				list.add(diDate);
			}
			return list;
		} catch (Exception e) {
			throw new Exception("Exception while retrieving "+name, e);
		} finally {
			if (rss != null)
				rss.close();
			if (pst != null)
				pst.close();
		}
	}
}
